﻿package mvc_view
{
	import flash.display.*;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.TextField;

	public class NerdsPageView extends View
	{
		private var _container:MovieClip;

		private var _avatarOne:MovieClip;
		private var _avatarTwo:MovieClip;
		private var _avatarThree:MovieClip;
		private var _avatarFour:MovieClip;
		private var _avatarFive:MovieClip;
		private var _avatarSix:MovieClip;

		private var _loaderOne:Loader;
		private var _loaderTwo:Loader;
		private var _loaderThree:Loader;
		private var _loaderFour:Loader;
		private var _loaderFive:Loader;
		private var _loaderSix:Loader;
		
		private var index:int = 0;

		public function NerdsPageView(container:MovieClip)
		{
			_container = container;

			_loaderOne = new Loader();
			_loaderOne.contentLoaderInfo.addEventListener(Event.COMPLETE, imageOneLoaded);
			_loaderTwo = new Loader();
			_loaderTwo.contentLoaderInfo.addEventListener(Event.COMPLETE, imageTwoLoaded);
			_loaderThree = new Loader();
			_loaderThree.contentLoaderInfo.addEventListener(Event.COMPLETE, imageThreeLoaded);
			_loaderFour = new Loader();
			_loaderFour.contentLoaderInfo.addEventListener(Event.COMPLETE, imageFourLoaded);
			_loaderFive = new Loader();
			_loaderFive.contentLoaderInfo.addEventListener(Event.COMPLETE, imageFiveLoaded);
			_loaderSix = new Loader();
			_loaderSix.contentLoaderInfo.addEventListener(Event.COMPLETE, imageSixLoaded);
	    }

		override public function update(event:Event = null):void
		{
			if (model.getAvatar(index) != null)
				_loaderOne.load(new URLRequest(model.getAvatar(index)));
				
			if (model.getAvatar(index+1) != null)
				_loaderTwo.load(new URLRequest(model.getAvatar(index+1)));
				
			if (model.getAvatar(index+2) != null)
				_loaderThree.load(new URLRequest(model.getAvatar(index+2)));
				
			if (model.getAvatar(index+3) != null)
				_loaderFour.load(new URLRequest(model.getAvatar(index+3)));
				
			if (model.getAvatar(index+4) != null)
				_loaderFive.load(new URLRequest(model.getAvatar(index+4)));
				
			if (model.getAvatar(index+5) != null)
				_loaderSix.load(new URLRequest(model.getAvatar(index+5)));

			// TODO: possibly put the text init above with check for nulls

			// set each nerd avatar information
			_container.nerdOne_txt.text = "User name: " + model.getUsername(index) + "\n" + model.getFirstName(index) + " " 
			+ model.getLastName(index) + "\n" + "Date of birth: " + model.getDataOfBirth(index) + "\n"
			+ "Joined: " + model.dateOfCreation(index);
			
			_container.nerdTwo_txt.text = "User name: " + model.getUsername(index + 1) + "\n" + model.getFirstName(index + 1) + " "
			+ model.getLastName(index + 1) + "\n" + "Date of birth: " + model.getDataOfBirth(index + 1) + "\n"
			+ "Joined: " + model.dateOfCreation(index + 1);
			
			_container.nerdThree_txt.text = "User name: " + model.getUsername(index + 2) + "\n" + model.getFirstName(index + 2) + " "
			+ model.getLastName(index + 2) + "\n" + "Date of birth: " + model.getDataOfBirth(index + 2) + "\n"
			+ "Joined: "+ model.dateOfCreation(index + 2);
			
			_container.nerdFour_txt.text = "User name: " + model.getUsername(index + 3) + "\n" + model.getFirstName(index + 3) + " "
			+ model.getLastName(index + 3) + "\n" + "Date of birth: " + model.getDataOfBirth(index + 3) + "\n"
			+ "Joined: "+ model.dateOfCreation(index + 3);
			
			_container.nerdFive_txt.text = "User name: " + model.getUsername(index + 4) + "\n" + model.getFirstName(index + 4) + " "
			+ model.getLastName(index + 4) + "\n" + "Date of birth: " + model.getDataOfBirth(index + 4) + "\n"
			+ "Joined: "+ model.dateOfCreation(index + 4);
			
			_container.nerdSix_txt.text = "User name: " + model.getUsername(index + 5) + "\n" + model.getFirstName(index + 5) + " "
			+ model.getLastName(index + 5) + "\n" + "Date of birth: " + model.getDataOfBirth(index + 5) + "\n"
			+ "Joined: "+ model.dateOfCreation(index + 5);
		}

		private function imageOneLoaded(event:Event):void
		{
			_container.nerdOne.addChild(_loaderOne);
		}

		private function imageTwoLoaded(event:Event):void
		{
			_container.nerdTwo.addChild(_loaderTwo);
		}
		
		private function imageThreeLoaded(event:Event):void
		{
			_container.nerdThree.addChild(_loaderThree);
		}
		
		private function imageFourLoaded(event:Event):void
		{
			_container.nerdFour.addChild(_loaderFour);
		}
		
		private function imageFiveLoaded(event:Event):void
		{
			_container.nerdFive.addChild(_loaderFive);
		}
		
		private function imageSixLoaded(event:Event):void
		{
			_container.nerdSix.addChild(_loaderSix);
		}
	}
}